#!/bin/bash
HOST_LIST="10.0.0.7 10.0.0.8"
function service_start() {
    for host in $HOST_LIST; do
        echo "--------------------Kakfa Start In Host $host--------------------"
        service_status $host
        if [ $? -eq 0 ]; then
            echo "Kafka broker in $host is already RUNNING"
        else
            echo "Now Kafka broker is STOPPED,Start it......"
            sshpass -proot ssh -o StrictHostKeyChecking=no root@$host "/opt/source/kafka/bin/kafka-server-start.sh -daemon /opt/source/kafka/config/server.properties" &>/dev/null
            index =0
            while [ $index -lt 5 ]; do
                service_status $host
                if [ $? -ne 0 ]; then
                    index=$(expr $index + 1)
                    echo "$index Times:Kafka broker in $host is starting failed... Please wait..."
                    echo "After 3 seconds will check kafka status again..."
                    sleep 3
                    continue
                else
                    echo "OK...Kafka broker in $host is RUNNING"
                    break
                fi
            done
            if [ $index -eq 5 ]; then
                echo "Sorry...Kafka broker Start Failed...Please login $host to check"
            fi
        fi
    done
}
function service_stop() {
    for host in $HOST_LIST; do
        echo "--------------------Kakfa Stop In Host $host--------------------"
        service_status $host
        if [ $? -ne 0 ]; then
            echo "Kafka broker in $host is already STOPPED"
        else
            echo "Now Kafka broker is RUNNINT,Stop it......"
            sshpass -proot ssh -o StrictHostKeyChecking=no root@$host "/opt/source/kafka/bin/kafka-server-stop.sh" &>/dev/null
            index =0
            while [ $index -lt 5 ]; do
                service_status $host
                if [ $? -eq 0 ]; then
                    index=$(expr $index + 1)
                    echo "$index Times:Kafka broker in $host is stopping failed... Please wait..."
                    echo "After 3 seconds will check kafka status again..."
                    sleep 3
                    continue
                else
                    echo "OK...Kafka broker in $host is STOPPED now..."
                    break
                fi
            done
            if [ $index -eq 5 ]; then
                echo "Sorry...Kafka broker Stop Failed...Please login $host to check"
            fi
        fi
    done
}
function service_status() {
    status_idx=0
    result=0
    while [ $status_idx -lt 3 ]; do
        sshpass -proot ssh -o StrictHostKeyChecking=no root@$1 "jps | grep -w Kafka" &>/dev/null
        if [ $? -eq 0 ]; then
            result=$(expr $result+1)
        fi
        status_idx=$(expr $status_idx+1)
    done

    if [ $result -eq 3 ]; then
        return
    fi
    return 99
}

function usage() {
    cat <<EOF
    Usage 1:bash $0 start
    Usage 2:bash $0 stop
    Usage 3:bash $0 status
EOF
}

case $1 in
start)
    service_start
    ;;

stop)
    service_stop
    ;;

status)
    for host in $HOST_LIST; do
        echo "--------------------Kakfa Status In Host $host--------------------"
        service_status $host
        if [ $? -eq 0 ]; then
            echo "Kafka broker in $host is RUNNING"
        else
            echo "Kafka broker in $host is STOPPED"
        fi
    done
    ;;
*)
    usage
    ;;

esac
